package com.imoblife.brainwavestus;

import java.util.ArrayList;

import com.imoblife.brainwavestus.adapter.ProductBuyAdapter;
import com.imoblife.brainwavestus.customview.ListViewItem;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItem;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItem.DownLoadingListener;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItemPragrame;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.user.User;
import com.imoblife.brainwavestus.util.BitMapFlyweightFactory;
import com.imoblife.brainwavestus.util.DBUtil;
import com.imoblife.brainwavestus.util.DebugUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ProductDetailActivityRestore extends BaseActivity {
	
	private ImageView mImgDetailIcon;
	private TextView mTvDetailTitle;
	private TextView mTvDetail;
	private ListView mLvProductBuy;
	
	private FrameLayout mContent;
	private Product mProduct;
	private ProductBuyAdapter mAdapter;
	private ArrayList<Product> mDatas;
	private BroadcastReceiver mReceiver;
	
	@Override
	protected void onCreate(Bundle arg0) {
		
		super.onCreate(arg0);
		
		if (getBrainWavesApplication().isPad()) {
			
			setContentView(R.layout.tb_purchase_product);
		} else {
			
			setContentView(R.layout.product_show_layout);
		}
		
		initView();		
		initReceiver();
	}	
	
	private void initReceiver() {
		
		mReceiver = new UpdateUIReceiver();
		
		IntentFilter filter = new IntentFilter();
		filter.addAction(Constant.UPDATE_PRODUCT_BUY_UI_ACTION);
		
		this.registerReceiver(mReceiver, filter);
	}
	
	private void unRegisterReceiver() {
		
		if(mReceiver != null) {
			
			this.unregisterReceiver(mReceiver);
		}
		
		DownLoadItem.setDownLoadingListener(null);
	}
	
	private void initView() {
		
		initTitleName(getString(R.string.store));
		String productName = getIntent()
					.getStringExtra(Constant.PRODUCT_ID);
		Store store = getBrainWavesApplication().getStore();
		mImgDetailIcon = (ImageView)findViewById(R.id.icon);
		mTvDetailTitle = (TextView) findViewById(R.id.title);
		mTvDetail = (TextView) findViewById(R.id.detail);
		mContent = (FrameLayout) 
				findViewById(R.id.swich_view_flipper);
		
		mImgDetailIcon.setImageBitmap(
					BitMapFlyweightFactory
					.getBitMapFlyweightFactory(getAssets())
					.getBitMapFlyweight(productName).getBitmap());
		
		mProduct = store.getProductByName(productName);
		ProductInfo info = mProduct.getProductInfo();
	
		mTvDetailTitle.setText(info.getDetailTitle());	
		mTvDetail.setText(info.getLongDescription());
		//mLvProductBuy = (ListView) findViewById(R.id.lv_product_buy);	
		mDatas = new ArrayList<Product>();
		
		if ( info.isProductCollection()) {
			
			mDatas.addAll(mProduct.getAllChild());
			
			if(info.getDownloadState().equals(
					Constant.DOWNLOAD_STATE_NOT_ENBLE)) {
				
				mDatas.add(mProduct);
			}
		}else {
			
			mDatas.add(mProduct);
		}	
		
		mAdapter = new ProductBuyAdapter(this, 
						R.layout.item_product_buy, 
						mDatas);
		
		User user = getBrainWavesApplication().getUser();
		mAdapter.setUser(user);
		mAdapter.setStore(store);
		mAdapter.setActivity(this);
		mLvProductBuy.setAdapter(mAdapter);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		setDownloadingListener();

	}

	private void setDownloadingListener() {
		
		DownLoadItem.setDownLoadingListener(new ListenerDownload());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unRegisterReceiver();
	}
	
	public void onIntroductionClicked(View v) {

		mContent.getChildAt(0).setVisibility(View.VISIBLE);
		mContent.getChildAt(1).setVisibility(View.GONE);
		mContent.getChildAt(2).setVisibility(View.GONE);
	}
	
	public void onPurchaseClicked(View v) {
		
		mContent.getChildAt(1).setVisibility(View.VISIBLE);
		mContent.getChildAt(0).setVisibility(View.GONE);
		mContent.getChildAt(2).setVisibility(View.GONE);
	}
	
	public void onGuaranteeClicked(View v) {
		
		DebugUtil.debug(TAG, "guarantee clicked");
		mContent.getChildAt(2).setVisibility(View.VISIBLE);
		mContent.getChildAt(0).setVisibility(View.GONE);
		mContent.getChildAt(1).setVisibility(View.GONE);
	}
	
	public void onGratitudeClicked(View v) {
		
		startActivity(new Intent(this,
				GratitudeActivity.class));
	}
	
	public class UpdateUIReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			
			DebugUtil.debug(TAG, "receive update ui msg now");
			ProductInfo info = mProduct.getProductInfo();
			
			if(info.isProductCollection()){
				
				mDatas.remove(mProduct);
			}				
			//mAdapter.clearAllItemsData();
			mAdapter.notifyDataSetChanged();
			mAdapter.updateUiList();
		}	
	}
	
	class ListenerDownload implements DownLoadingListener, Runnable {

		Handler handler = new Handler();
		ListViewItem mListViewItem;
		int extr;
		long progress;
		long size;
		int count = 0;

		public void run() {

			if (extr == DownLoadItemPragrame.EXTR_SUCC) {

				if (mListViewItem.mTaskState == DownLoadItemPragrame.DOWN_STATE_FAILE) {
					mListViewItem.updateUI();
				}

				if (count % 10 == 0) {

					mListViewItem.mDownloadBar
							.setMax((int) (size / 100));
					mListViewItem.mDownloadBar
							.setProgress((int) (progress / 100));

					float fomart = progress * 1.0f / size * 10000f;

					String sub = null;

					if ((int) (fomart % 100) >= 10) {
						sub = (int) (fomart % 100) + "%";
					} else {
						sub = "0" + (int) (fomart % 100) + "%";
					}

					mListViewItem.mDownloadtext.setText((int) (progress
							* 1.0f / size * 100f)
							+ "." + sub);

				}
				count++;

				if (progress == size) {
					count = 0;
					handler.postDelayed(new Runnable() {
						public void run() {
							mAdapter.updateUiList();
						}
					}, 1000);
				}
			} else {
				count = 0;
				handler.postDelayed(new Runnable() {
					public void run() {
						mAdapter.updateUiList();
					}
				}, 1000);

			}
		}

		public void downloading(String downlaodname, int extr,
				long progress, long size) {

			mListViewItem = mAdapter.getMap().get(downlaodname.replace(".music",
					""));

			if (mListViewItem == null) {
				return;
			}

			this.extr = extr;
			this.progress = progress;
			this.size = size;
			handler.post(this);

		}

	};
	

}
